// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import 'element-ui/lib/theme-default/index.css'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import axios from 'axios'
import {getUserInfo} from './util/auth.js'
Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.use(ElementUI)
/* eslint-disable no-new */
// let whiteList = ['/login']

router.beforeEach((to, from, next) => {
    let userInfo = getUserInfo()
    if (userInfo) {
        next()
    } else {
        if (store.getters.userName === '') {
            store.dispatch('login', userInfo).then(() => {
                next()
            }).catch(err => {
                console.error(err)
                next()
            })
        } else {
            next()
        }
    }
})

router.beforeEach((to, from, next) => {
    if (store.getters.menuResource === null) {
        store.dispatch('get_menu').then(() => {
            next()
        }).catch(err => {
            console.error(err)
            window.location.href = '/login'
            next()
        })
    } else {
        next()
    }
})

new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: { App }
})
